package badugaclient;

import java.awt.EventQueue;
import java.io.IOException;
import java.net.UnknownHostException;

public class ClientSwitch  implements IObserver, Information
{
	public PlayerInfo p;
	public GuiCards gc;
	public Gui gui;
	public GuiStart gs;
	//TODO public GUI
	private ClientServerCom com;
	
	ClientSwitch()
	{
		this.p = new PlayerInfo();
		//TODO this.GUI = new GUI();
		this.com = new ClientServerCom();
		//TODO GUI.addObserver(this);
		this.gs = new GuiStart(p);
		
		this.gui = new Gui(p);
		
		this.gc = new GuiCards();
		
		gs.addObserver(ClientSwitch.this);
		gs.setVisible(true);
	}
	
	public void messageReceived(String inFromServer)
	{
		try
		{
			com.getWhatIsMine((Information) this);
			p.setAction(com.checkAction(inFromServer));
			com.printClient("Got news for you: " + inFromServer);
			
			switch(p.getAction())
			{
				case "INI":
					System.out.println("JESTEM2TUTAJ !!!!");
					if(inFromServer.substring(6,7).equals("?"))
					{
						System.out.println("JESTEM TUTAJ !!!!");
					  	//GUI.canWeStart();
						//gs.LetsStartGame();
					}
					else
					{
						//pInfo.setInfo(com.display());
						System.out.println("JESTEM 3TUTAJ !!!!");
					}
					
					break;
				case "BEG":
					com.printClient(inFromServer);
					p.setHand(com.getMsgFromServer());
					gc.getHandFromCS(p.getHand());
					/* 
					 * GUI.setHand(); DONE
					 * GUI.show("Cards exchanged.");
					 */
					break;
				case "DBS":
					com.printClient("Dealer, big blind, small blind holder as follows: "+inFromServer.substring(6));
					p.setDealerID(inFromServer.substring(6,7));
					p.setBigID(inFromServer.substring(8, 9));
					p.setSmallID(inFromServer.substring(10, 11));
					p.setCurrentTopBet(p.getBigBlind());
					if(p.getBigID().equals(p.getMyID()))
					{
						/* GUI.playBigBlind() */
					}
					else if(p.getSmallID().equals(p.getMyID()))
					{
						/* GUI.playSmallBlind() */
					}
					/* GUI.setDealerSmallAndBig(get it from pInfo) */
					//gs.setVisible(false);
					break;
				case "AUC":
					com.printClient("We are currently in " + inFromServer.substring(6) + " betting round.");
					switch(inFromServer.substring(4,6))
					{
						case "__":
							p.setBetRound(inFromServer.substring(6, 7));
							/* GUI switch viewing round - player is waiting */
							break;
						case "ur":
							/* GUI switch viewing round - players move
							 * GUI enable buttons
							 */
							break;
						case "ch":
							p.setPlayersID(inFromServer.substring(6));
							if(p.getMyID().equals(p.getPlayersID()))
							{
								/* GUI - player (me) played check */
								com.printClient("Check.");
							}
							else
							{
								/* GUI - player played check */
								com.printClient("Player " + p.getPlayersID() + ": Check.");
							}
							break;
						case "be":
							p.setPlayersID(inFromServer.substring(6, 7));
							p.setPlayersBet(inFromServer);
							p.setPlayersTokens(inFromServer);
							p.setBetted(true);
							if(p.getSmallID().equals(p.getPlayersID()))
							{
								p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
							}
							else if(p.getBigID().equals(p.getMyID()))
							{
								p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
							}
							else
							{
								p.setCurrentTopBet(Integer.toString(p.getPlayersBet()));
							}
							com.printClient("Current max bet: " + p.getCurrentTopBet());
							if(p.getMyID().equals(p.getPlayersID()))
							{
								if(p.getSmallID().equals(p.getMyID()))
								{
									p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
								}
								else if(p.getBigID().equals(p.getMyID()))
								{
									p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
								}
								else
								{
									p.setMyBetInt(Integer.toString(p.getMyBetInt()));
								}
								p.setMyTokens(Integer.parseInt(p.getPlayersTokens()));
								/* GUI - player (me) played bet */
								com.printClient("Bet.");
							}
							else
							{
								/* GUI - player played bet */
								com.printClient("Player " + p.getPlayersID() + ": Bet.");
							}
							break;
						case "ra":
							p.setPlayersID(inFromServer.substring(6, 7));
							p.setPlayersBet(inFromServer);
							p.setPlayersTokens(inFromServer);
							p.setRaised(true);
							if(p.getSmallID().equals(p.getPlayersID()))
							{
								p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
							}
							else if(p.getBigID().equals(p.getMyID()))
							{
								p.setCurrentTopBet(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
							}
							else
							{
								p.setCurrentTopBet(Integer.toString(p.getPlayersBet()));
							}
							com.printClient("Current max bet: " + p.getCurrentTopBet());
							if (p.getMyID().equals(p.getPlayersID()))
							{
								if(p.getSmallID().equals(p.getMyID()))
								{
									p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getSmallBlind())));
								}
								else if(p.getBigID().equals(p.getMyID()))
								{
									p.setMyBetInt(Integer.toString(p.getPlayersBet() + Integer.parseInt(p.getBigBlind())));
								}
								else
								{
									p.setMyBetInt(Integer.toString(p.getMyBetInt()));
								}
								p.setMyTokens(Integer.parseInt(p.getPlayersTokens()));
								/* GUI - player (me) played raise */
								com.printClient("Raise.");
							}
							else
							{
								/* GUI - player played bet */
								com.printClient("Player " + p.getPlayersID() + ": Raise.");
							}
							/* GUI - refresh panel with bank */
							break;
						case "ca":
							p.setPlayersID(inFromServer.substring(6, 7));
							p.setPlayersTokens(inFromServer.substring(7));
							if(p.getMyID().equals(p.getPlayersID()))
							{
								p.setMyBetInt(Integer.toString(p.getCurrentTopBet()));
								p.setMyTokens(Integer.parseInt(p.getPlayersTokens()));
								/* GUI - player (me) played call */
								com.printClient("Call.");
							}
							else
							{
								/* GUI - player played call */
								com.printClient("Player " + p.getPlayersID() + ": Call.");
							}
							break;
						case "fo":
							p.setPlayersID(inFromServer.substring(6, 7));
							if(p.getMyID().equals(p.getPlayersID()))
							{
								/* GUI - player (me) played fold */
								com.printClient("Fold.");
							}
							else
							{
								/* GUI - player played fold */
								com.printClient("Player " + p.getPlayersID() + ": Fold.");
							}
						case "al":
							p.setPlayersID(inFromServer.substring(6, 7));
							p.setPlayersBet(inFromServer.substring(7));
							if(p.getMyID().equals(p.getPlayersID()))
							{
								p.setMyBetInt(Integer.toString(p.getPlayersBet() + p.getMyBetInt()));
								p.setMyTokens(0);
								/* GUI - player (me) played all-in */
								com.printClient("All-In.");
							}
							else
							{
								/* GUI - player played all-in */
								com.printClient("Player " + p.getPlayersID() + ": All-In.");
							}
							break;
					}
					break;
				case "CHG":
					com.printClient("Card exchange phase.");
					switch(inFromServer.substring(4,6))
					{
						case "__":
							/* GUI - waiting for others to finish */
							break;
						case "os":
							/* GUI - cards exchanging phase for player */
							break;
						default:
							p.setPlayersCardChange(inFromServer);
							/* GUI - refresh */
					}
				case "WON":
					com.printClient("Ladies and gentleman, and the winner is...");
					switch(inFromServer.substring(4, 6))
					{
						case "on":
							p.setHowManyWinners("1");
							p.setWinnerID(inFromServer.substring(6, 7));
							p.setWinningAmount(inFromServer.split("|")[1]);
							if(p.getWinnerID().equals(p.getMyID()))
							{
								p.setMyTokensAfterAll(Integer.parseInt(p.getWinningAmount()));
							}
							/* GUI - show winner */
							com.printClient("Player " + p.getWinnerID()+"! Congrats! You won " + p.getWinningAmount() + "tokens!");
							break;
						case "dr":
							p.setHowManyWinners(Integer.toString(inFromServer.split("|")[0].substring(6).length()));
							p.setWinnersID(inFromServer.split("|")[0].substring(6));
							p.setWinningAmount(inFromServer.split("|")[1]);
							for(int i=0; i<p.getHowManyWinners(); i++)
							{
								String temp = p.getWinnersID().substring(i, i+1);
								if(temp.equals(p.getMyID()))
								{
									p.setMyTokensAfterAll(Integer.parseInt(p.getWinningAmount()));
								}
							}
							/* GUI - show the winner */
							com.printClient("It was a draw between " + p.getWinnersID()+"! Each of them won " + p.getWinningAmount()+" tokens! Congrats!");
						break;	
					}
					break;
				case "NXT":
					com.printClient("Do you wish to continue?");
					switch(inFromServer.substring(6, 8))
					{
						case "YN":
							/* GUI - ask to continue */
							break;
						case "PR":
							p.setLooserID(inFromServer.substring(8));
							/* GUI - tell player that he loose */
							break;
						case "ED":
							/* GUI - end of game */
					}
					break;
				case "ERR":
					com.printClient(inFromServer.substring(6));
					break;
			}
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
	}
	
	@Override
	public void updateGUI()
	{
		switch(p.getContent())
		{
			case "Connect":
				
				
				try
				{
					
					com.connect(p.getHost(), p.getConnectionPort());
					/* GUI TODO - change con to dc button */
					gs.changeToDisconnect();
					p.setAction(com.checkAction());
					com.getWhatIsMine((Information) this);
					if(p.getAction().equals("INI"))
					{
						p.setStartingParam(com.getMsgFromServer());
						com.printClient("Player ID: " +p.getMyID());
						gs.LetsStartGame();
					}
					else if(p.getAction().equals("ERR"))
					{
						/* TODO GUI - change dc to con button */
						gs.changeToconnect();
					}
				
				}
				catch(UnknownHostException uhe)
				{
					System.err.println("Cannot recognize host.");
					uhe.printStackTrace();
				}
				catch(IOException ioe)
				{
					System.err.println("I/O failed. Printing where the problem is.");
					ioe.printStackTrace();
				}
				catch(NullPointerException npe)
				{
					System.err.println("Don't try to read from something that, don't exists!");
					npe.printStackTrace();
				}
				if(p.getAction() == null)
					System.out.println("BUUUUUUUUUUUUG");
				System.out.println(p.getAction());
				break;
				case "Disconnect":
				try
				{
					p.setSentToServer(p.getMyID()+"DCN___");
					com.sendMessage(p.getSentToServer());
					com.stopListen();
					com.printClient("Disconnecting...");
					gs.changeToconnect();
					p.setInfo(com.disconnect());
					com.printClient("I'm outa' here...");
				}
				catch(IOException ioe)
				{
					System.err.println("I/O failed. Printing where the problem is.");
					ioe.printStackTrace();
				}
				break;
			case "Start game":
			
				System.out.println("DEBUG !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
				com.sendMessage(p.getSentToServer());
				/* TODO GUI - start game */
				gs.setVisible(false);
		
				break;
			case "Check":
				p.setSentToServer(p.getMyID()+"AUCch_");
				com.sendMessage(p.getSentToServer());
				com.printClient("Check. " + p.getSentToServer());
				/* TODO GUI - buttons off */
				break;
			case "Bet":
				p.setSentToServer(p.getMyID()+"AUCbe"+p.getMyBet());
				com.sendMessage(p.getSentToServer());
				com.printClient("Bet. " + p.getSentToServer());
				/* TODO GUI - buttons off */
				break;
			case "Raise":
				p.setSentToServer(p.getMyID()+"AUCra"+p.getMyRaise());
				com.sendMessage(p.getSentToServer());
				com.printClient("Raise. " + p.getSentToServer());
				/* TODO GUI - buttons off */
				break;
			case "Call":
				p.setSentToServer(p.getMyID()+"AUCca"+p.getCall());
				com.sendMessage(p.getSentToServer());
				com.printClient("Call. " + p.getSentToServer());
				/* TODO GUI - buttons off */
				break;
			case "Fold":
				p.setSentToServer(p.getMyID()+"AUCfo_");
				com.sendMessage(p.getSentToServer());
				com.printClient("Fold. " + p.getSentToServer());
				/* TODO GUI - buttons off */
				break;
			case "All-in":
				p.setSentToServer(p.getMyID()+"AUCal"+p.getMyTokens());
				com.sendMessage(p.getSentToServer());
				com.printClient("All-in. " + p.getSentToServer());
				/* TOD GUI - buttons off */
				break;
				
			case "CARD_0":
				p.setCardsToChange("T", 0);
				/* TODO GUI - do sth with that */
				break;
			case "CARD_1":
				p.setCardsToChange("T", 1);
				/* TODO GUI - do sth with that */
				break;
			case "CARD_2":
				p.setCardsToChange("T", 2);
				/* TODO GUI - do sth with that */
				break;
			case "CARD_3":
				p.setCardsToChange("T", 3);
				/* TODO GUI - do sth with that */
				break;
			case "Exchange":
				p.setSentToServer(p.getMyID()+"CHG__"+p.getCardsToChange());
				com.sendMessage(p.getSentToServer());
				com.printClient("Changing cards. " +p.getSentToServer());
				p.resetSelection();
				/* TODO GUI - disable selecting card */
				break;
			case "Yes":
				/* TODO GUI - reset (player want to continue */
				p.defaultState();
				com.sendMessage(p.getMyID()+"NXT__Y");
				break;
			case "No":
				com.sendMessage(p.getMyID()+"NXT__N");
				com.stopListen();
				System.exit(0);
				break;
		}
	}
	
	
	
	
}
